var express = require('express');
var router = express.Router();
var hget=require('../util/http_get_util.js');
var hpost=require('../util/http_post_util.js');
router.get('/',function(req,res,next){
    hget("http://localhost:9999/shangcheng").then(function (data) {
      var obj=JSON.parse(data);//data是json字符串
      if(obj.status==1){//成功
        res.render('shangcheng',obj);
      }else if(obj.status==2){//失败
        console.log(obj.message);
      }
    },function (e) {
      console.log(e);
    });
  });

//将详情页与数据库连接起来（每个图片都不一样）
  router.get('/xiangqing', function(req, res, next) {
    hget(`http://localhost:9999/xiangqing?id=${req.param("id")}`).then(function (data) {
    	console.log(data)
    /*hget("http://localhost:9999/xiangqing?id=${req.param("id")}").then(function (data) {*/
      var obj=JSON.parse(data);
      if(obj.status==1){
        res.render('xiangqing',obj);
      }else if(obj.status==2){
        console.log(obj.message);
      }
    },function (e) {
      console.log(e.statusCode,e.statusMessage);
    });
  });


//搜索框的处理逻辑
 router.post('/sousuo', function(req, res, next) {
    hpost("/submit_sousuo",req.body).then(function (data) {
      var obj=JSON.parse(data);
      if(obj&&obj.status==1){//成功应该显示对应的商品页面
        res.redirect('/one/aabbcc');
      }else{
        res.send("<h1>修改失败</h1>");
      }
    },function (e) {
      console.log(e);
      console.log(e.statusCode,e.statusMessage);
    });
  });

 module.exports=router;